<template>
	<view>
		<view class="page-body">
			<view class="page-section page-section-gap">
				<map style="width: 100%; height: 300px;" :latitude="latitude" :longitude="longitude" scale="16"
					:markers="markers" show-location="true" @markertap="markertap" v-if="isShow"></map>
			</view>
		</view>
	</view>
</template>

<script>
	import amap from "../../js_sdk/amap-wx.js"
	export default {
		data() {
			return {
				markers: [{}, {}, {}],
				poisdatas: [{}, {}, {}],
				title: 'map',
				isShow: false,
				amapPlugin: null
				// latitude: 30.208487,
				// longitude: 120.21202
			};
		},
		onLoad() {
			var that = this;
			this.amapPlugin = new amap.AMapWX({
				key: 'a86784ca2e76de292448c052162f35a1'
			});
			that.getLocation();
		},
		methods: {
			//得到点击的marker的id，遍历markers数组，判断有没有相等的id
			//如果有的就能从this.poisdatas[i].address得到坐标位置（没有就提醒下）
			// markertap: function(e) {
			// 	for (var i = 0; i < this.markers.length; i++) {
			// 		if (JSON.stringify(e).substring(18, 20) == this.markers[i].id) {
			// 			console.log('markers' + this.poisdatas[i].name + '   ' + this.poisdatas[i].address);
			// 			uni.showToast({
			// 				title: this.poisdatas[i].name,
			// 				mask: false,
			// 				duration: 1500
			// 			});
			// 		}
			// 	}
			// }
			getLocation() {
				let _this = this;
				let opts = {
					url: 'Jz/SearchZb.shtml'
				};
				let param = {
					user_id: uni.getStorageSync('classUserId'),
					userId: uni.getStorageSync('userInfo').userId,
					authToken: uni.getStorageSync('userInfo').token
				};
				_this.$http.httpRequest(opts, param).then(
					res => {
						console.log(res.data);
						if (res.data.code == '200') {
							if (res.data.latitude != '' || res.data.longitude != '') {
								let data = res.data.data;
								console.log(data);
								_this.latitude = data[0].latitude;
								_this.longitude = data[0].longitude;
								let param = {
									id: 1,
									latitude: data[0].latitude,
									longitude: data[0].longitude,
									iconPath: '../../static/img/ic_default_head.png',
									width: '30px',
									height: '30px'
								};
								_this.markers.push(param);
								_this.isShow = true;
							} else {
								uni.getLocation({
									type: 'wgs84',
									success: function(res) {
										console.log('当前位置的经度：' + res.longitude);
										console.log('当前位置的纬度：' + res.latitude);
										_this.latitude = res.latitude;
										_this.longitude = res.longitude;
										let param = {
											latitude: res.latitude,
											longitude: res.longitude,
											iconPath: '../../static/img/ic_default_head.png',
											width: '60px',
											height: '60px'
										};
										_this.markers.push(param);
										_this.isShow = true;
									}
								});
							}
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
						}
					},
					error => {
						console.log(error);
					}
				);
			}
		}
	};
</script>

<style>
	/deep/.csssprite {
		display: none !important;
	}
</style>
